package cn.ranyue.org.service.impl;

import cn.ranyue.basic.service.impl.BaseServiceImpl;

import cn.ranyue.org.domain.Department;
import cn.ranyue.org.mapper.DepartmentMapper;
import cn.ranyue.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired()
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void add(Department department) {

        departmentMapper.add(department);
        this.update(department);
    }


    @Override
    @Transactional
    public void update(Department department) {
        String selfDirPath = "";
        if(department.getParent()==null){//是顶级部门
            selfDirPath = "/"+department.getId();
        }else {//如果不是顶部门
            Department parent = departmentMapper.loadOne(department.getParent().getId());//查出父级部门
            selfDirPath = parent.getDirPath() + "/" + department.getId();//自己的id
        }
        department.setDirPath(selfDirPath);
        departmentMapper.update(department);
    }

    /*查选部门树*/
    @Override
    public List<Department> getTree() {
        /*使用map方式查询*/

        //1查选出所有部门
        List<Department> all = departmentMapper.loadAll();
        //把部门转化为map key是部门id value就是这个部门对象
        //Map<Long, Department> map = all.stream().collect(Collectors.toMap(Department::getId, item -> item));
        HashMap<Long, Department> map = new HashMap<>();
        for (Department department : all) {
            map.put(department.getId(),department);
        }
        //声明要返回的集合 装顶级部门
        List<Department> result = new ArrayList<>();
        //2 遍历所有如果是顶级部门就放进集合中
        for (Department department : all) {
            if(department.getParent_id()==null){
                result.add(department);
            }else{
               Long pid = department.getParent_id();
                Department parent = map.get(pid);
                if(parent != null){//防止脏数据
                    List<Department> children = parent.getChildren();
                    children.add(department);
                }
            }
        }
        //如果不是顶级部门就找到自己的父级部门 把自己装进父部门的children集合中
        return result;


        /*嵌套循环*/
        //1查选出所有部门
    /*    List<Department> all = departmentMapper.loadAll();
        //声明要返回的集合 装顶级部门
        List<Department> result = new ArrayList<>();
        //2 遍历所有如果是顶级部门就放进集合中
        for (Department department : all) {
            if(department.getParent_id()==null){
                result.add(department);
            }else{
                for (Department parent : all) {
                    if(department.getParent_id()==parent.getId()){//找到了父级部门
                      List<Department> children = parent.getChildren();
                        children.add(department);//把自己添加到父级部门
                        break;//结束循环
                    }
                }
            }
        }
        //如果不是顶级部门就找到自己的父级部门 把自己装进父部门的children集合中
        return result;*/
    }


}
